#!/usr/bin/env python
# encoding: utf-8
"""
@author: youfeng
@email: youfeng243@163.com
@license: Apache Licence
@file: main.py
@time: 2017/12/20 14:59
"""
from multiprocessing import Process

import click

from config.beanstalk_conf import LISTEN_BEANSTALK_CONF
from config.proxy_conf import GateWayType
from logger import AppLogger
from worker.list_worker import ListWorker

# 日志模块
global_log = AppLogger('gsxt_online_crawl.log').get_logger()


# 获得子进程日志名称
def get_process_log_name(input_tube, proxy_type, process_id):
    return '{}-worker-{}-{}.log'.format(input_tube, proxy_type, process_id)


# 进程启动入口
def process_start(thread_num, input_tube, store_tube, seed_table, process_id, proxy_type):
    log_name = get_process_log_name(input_tube, proxy_type, process_id)
    log = AppLogger(log_name).get_logger()
    log.info("当前进程: process_id = {}".format(process_id))
    log.info("当前进程协程数目: thread_num = {}".format(thread_num))
    log.info("当前进程请求消息队列: tube = {}".format(input_tube))
    log.info("当前进程存储消息队列: tube = {}".format(store_tube))
    log.info("当前进程存储种子表: tube = {}".format(seed_table))
    log.info("当前进程代理类型: {}".format(proxy_type))
    detail_worker = ListWorker(thread_num, input_tube, store_tube,
                               seed_table, process_id, LISTEN_BEANSTALK_CONF,
                               proxy_type, log=log)
    detail_worker.start()


@click.command()
@click.option('--process_num',
              default=1,
              type=int,
              help='进程数目')
@click.option('--thread_num',
              default=1,
              type=int,
              help='协程数目')
@click.option('--input_tube',
              default='online_crawl_gsxt',
              help='发送列表页抓取请求消息队列')
@click.option('--store_tube',
              default='gsxt_saver',
              help='存储消息队列')
@click.option('--seed_table',
              default='business_online_company_list',
              help='存储消息队列')
@click.option('--proxy_type',
              default=GateWayType.GATEWAY_YIZHOU,
              help='代理类型')
def main(process_num, thread_num, input_tube, store_tube, seed_table, proxy_type):
    process_list = []

    for process_id in xrange(process_num):
        process_handler = Process(target=process_start,
                                  args=(thread_num, input_tube, store_tube, seed_table, process_id, proxy_type,))
        process_handler.start()
        process_list.append(process_handler)

    global_log.info("列表页进程组初始化完成...")
    for _process in process_list:
        _process.join()


if __name__ == '__main__':
    main()
